/**
 * @projectName gs_online
 * @package com.answer.wx.gateway.service.impl
 * @className com.answer.wx.gateway.service.impl.CommentServiceImpl
 */
package com.answer.wx.gateway.service.impl;

import com.answer.wx.common.enums.ErrorEnum;
import com.answer.wx.common.exception.RequestException;
import com.answer.wx.dao.mapper.*;
import com.answer.wx.dao.model.*;
import com.answer.wx.gateway.service.CommentService;
import com.answer.wx.gateway.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * CommentServiceImpl
 * @description 评论实现类
 * @author answer_wx
 * @date 2022/8/17 16:19
 * @version 1.0
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentReplyMapper commentReplyMapper;
    @Autowired
    private NewMessageMapper newMessageMapper;
    @Autowired
    private AttendMapper attendMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private UserService userService;

    @Transactional
    @Override
    public void deleteCommentByCommentId(Integer userId, Integer commentId) {
        User user = userService.getById(userId);
        if (user == null) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户不存在！");
        }
        Comment comment = new Comment();
        comment.setCommentId(commentId);
        if (user.getUserIsAdmin() != 2) {
            comment.setUserId(userId);
        }
        commentMapper.deleteByPrimaryKey(comment);
        CommentReply commentReply = new CommentReply();
        commentReply.setCommentId(commentId);
        commentReplyMapper.delete(commentReply);
    }

    @Override
    public void deleteCommentReplyById(Integer userId, Integer commentReplyId) {
        User user = userService.getById(userId);
        if (user == null) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户不存在！");
        }
        CommentReply commentReply = new CommentReply();
        if (user.getUserIsAdmin() != 2) {
            commentReply.setReplayUserId(userId);
        }
        commentReply.setCommentReplyId(commentReplyId);
        commentReplyMapper.delete(commentReply);
    }

    @Override
    public void addCommentReply(Integer messageId, CommentReply commentReply) {
        User user = userService.getById(commentReply.getReplayUserId());
        if (user == null) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户不存在！");
        }
        if (user.getUserAllow() != 1) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户被禁止");
        }

        commentReplyMapper.insertSelective(commentReply);
        if (commentReply.getReceiveUserId().equals(commentReply.getReplayUserId())) {
            return;
        }

        if (!commentReply.getReplayUserId().equals(commentReply.getReceiveUserId())) {
            NewMessage newMessage = new NewMessage();
            newMessage.setUserId(commentReply.getReceiveUserId());
            newMessage.setNewMessageType(2);
            newMessage.setMessageId(messageId);
            newMessage.setNewMessageDetail(commentReply.getReplyDetail());
            newMessageMapper.insertSelective(newMessage);
        }

        Attend attend = new Attend();
        attend.setMessageId(messageId);
        attend.setUserId(commentReply.getReplayUserId());

        if (attendMapper.selectCount(attend) != 0) {
            return;
        }
        attendMapper.insertSelective(attend);
    }

    @Override
    public void addCollection(Integer userId, Integer messageId) {
        User user = userService.getById(userId);
        if (user == null) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户不存在！");
        }

        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setMessageId(messageId);
        collectMapper.insertSelective(collect);
    }

    @Override
    public void deleteCollection(Integer userId, Integer messageId) {
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setMessageId(messageId);
        collectMapper.delete(collect);
    }

    @Override
    public int checkIsCollection(Integer userId, Integer messageId) {
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setMessageId(messageId);
        return collectMapper.selectCount(collect);
    }

    @Override
    public void addComment(Integer userId, String userComment, Integer messageId) {
        User user = userService.getById(userId);
        if (user == null) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户不存在！");
        }
        if (user.getUserAllow() != 1) {
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "用户被禁止");
        }
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if (message == null){
            throw new RequestException(ErrorEnum.OPERATION_FAIL, "信息为空");
        }
        Comment comment = new Comment();
        comment.setMessageId(messageId);
        comment.setUserId(userId);
        comment.setCommentDetail(userComment);
        commentMapper.insertSelective(comment);

        if(!userId.equals(message.getUserId())) {
            NewMessage newMessage = new NewMessage();
            newMessage.setUserId(message.getUserId());
            newMessage.setNewMessageType(1);
            newMessage.setNewMessageDetail(userComment);
            newMessage.setMessageId(messageId);
            newMessageMapper.insertSelective(newMessage);
        }
        Attend attend = new Attend();
        attend.setMessageId(messageId);
        attend.setUserId(userId);
        if (attendMapper.selectCount(attend) != 0) {
            return;
        }
        attendMapper.insertSelective(attend);
    }
}